package anyVehicle;

import java.util.ArrayList;
import java.util.List;

import net.minecraft.src.GuiButton;
import net.minecraft.src.GuiScreen;
import net.minecraft.src.GuiTextField;

public class GUIScaffold extends GuiScreen {
	private TileEntityScaffold scaffoldTE;
	private List<GuiTextField> tfs = new ArrayList<GuiTextField>();
	private GuiButton buttonExit;
	private GuiTextField tfWidth;
	private GuiTextField tfHeight;
	private GuiTextField tfLength;
	private GuiButton buttonCreateBoat;

	public GUIScaffold(TileEntityScaffold scaffoldTE) {
		this.scaffoldTE = scaffoldTE;
	}

	public void initGui() {
		buttonExit = new GuiButton(2, 60, height / 2 + 40, 100, 20, "Back to Game");
		buttonCreateBoat = new GuiButton(2, 200, height / 2 + 40, 150, 20, "Create CustomBoat");
		controlList.add(buttonExit);
		controlList.add(buttonCreateBoat);
		tfWidth = new GuiTextField(this, fontRenderer, 100, 30, 40, 20, String.valueOf(scaffoldTE.getWidthBlocks()-2));
		tfs.add(tfWidth);
		tfHeight = new GuiTextField(this, fontRenderer, 100, 55, 40, 20, String.valueOf(scaffoldTE.getHeightBlocks()-2));
		tfs.add(tfHeight);
		tfLength = new GuiTextField(this, fontRenderer, 100, 80, 40, 20, String.valueOf(scaffoldTE.getLengthBlocks()-2));
		tfs.add(tfLength);

	}

	public boolean doesGuiPauseGame() {
		return false;
	}

	public void drawScreen(int i, int j, float f) {
		super.drawScreen(i, j, f);
		for (int k = 0; k < tfs.size(); k++) {
			GuiTextField tf = (GuiTextField) tfs.get(k);
			tf.drawTextBox();
		}
		drawString(fontRenderer, "Width :", 60, 35, 0xffffff);
		drawString(fontRenderer, "Height:", 60, 60, 0xffffff);
		drawString(fontRenderer, "Length:", 60, 85, 0xffffff);

	}

	@Override
	protected void mouseClicked(int i, int j, int k) {
		super.mouseClicked(i, j, k);
		if (k == 0) {
			for (GuiTextField tf : tfs) {
				tf.mouseClicked(i, j, k);
			}
		}

	}

	protected void keyTyped(char c, int i) {
		super.keyTyped(c, i);
		System.out.println((int)c + " " + i);
		if (i == 15 || i == 28) {
			for (GuiTextField tf : tfs) {
				if (tf.isEnabled && tf.isFocused) {
					tf.setFocused(false);
					int pos = tfs.indexOf(tf);
					pos++;
					if (pos >= tfs.size()) {
						pos = 0;
					}
					tfs.get(pos).setFocused(true);
					return;
				}
			}
			tfs.get(0).setFocused(true);
		} else if ((i >=0 && i <= 11) || i == 14) { // Only allow numbers and backspace
			for (GuiTextField tf : tfs) {
				tf.textboxKeyTyped(c, i);
				if (tf.getText().length() > 2) {
					tf.setText(tf.getText().substring(0,2));
				}
			}
		}

	}

	@Override
	protected void actionPerformed(GuiButton guibutton) {
		super.actionPerformed(guibutton);
		if (guibutton == buttonExit) {
			mc.displayGuiScreen(null);
			mc.setIngameFocus();
		} else if (guibutton == buttonCreateBoat) {
			updateScaffoldSize();
			scaffoldTE.createCustomBoat();
			mc.displayGuiScreen(null);
			mc.setIngameFocus();
		}
	}

	@Override
	public void onGuiClosed() {
		super.onGuiClosed();

		updateScaffoldSize();

	}

	public void updateScaffoldSize() {
		try {
			scaffoldTE.setWidthBlocks(Integer.valueOf(tfWidth.getText())+2);
		} catch (NumberFormatException e) {
		}

		try {
			scaffoldTE.setHeightBlocks(Integer.valueOf(tfHeight.getText())+2);
		} catch (NumberFormatException e) {
		}

		try {
			scaffoldTE.setLengthBlocks(Integer.valueOf(tfLength.getText())+2);
		} catch (NumberFormatException e) {
		}
	}

}
